package TUILayer;

import java.util.ArrayList;

import ControlLayer.*;
import ModelLayer.RealException;

public class GroupUI {

	private GroupCtr groupCtr;
	private InputParser inputParser;
	private LoginInfo loginInfo;

	public GroupUI(InputParser inputParser, LoginInfo loginInfo) {
		groupCtr = new GroupCtr();
		this.inputParser = inputParser;
		this.loginInfo = loginInfo;
	}

	public void start() throws RealException {
		int choise = 0;
		boolean exit = false;
		switch (loginInfo.getLoggedEmployeePositionId()) {
		case 1: {
			while (!exit) {
				choise = inputParser
						.getInt("\f *** GroupMenu ***\n\n1. Show group information\n2. Create group\n3. Update group\n4. Delete group\n5. List all groups\n6. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					showGroup();
					break;
				}
				case 2: {
					createGroup();
					break;
				}
				case 3: {
					updateGroup();
					break;
				}
				case 4: {
					deleteGroup();
					break;
				}
				case 5: {
					listAllGroups();
					break;
				}
				case 6: {
					exit = true;
					break;
				}
				}
			}
			break;
		}
		case 2: {
			while (!exit) {
				choise = inputParser
						.getInt("\f *** GroupMenu ***\n\n1. Show group information\n2. Create group\n3. Update group\n4. Delete group\n5. List all groups\n6. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					showGroup();
					break;
				}
				case 2: {
					createGroup();
					break;
				}
				case 3: {
					updateGroup();
					break;
				}
				case 4: {
					deleteGroup();
					break;
				}
				case 5: {
					listAllGroups();
					break;
				}
				case 6: {
					exit = true;
					break;
				}
				}
			}
			break;
		}
		case 3: {
			while (!exit) {
				choise = inputParser
						.getInt("\f *** GroupMenu ***\n\n1. Show group information\n2. List all groups\n3. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					showGroup();
					break;
				}
				case 2: {
					listAllGroups();
					break;
				}
				case 3: {
					exit = true;
					break;
				}
				}
			}
		}
		}
	}

	private void showGroup() throws RealException {
		int id = inputParser
				.getInt("\f ***ShowGroupMenu ***\n\nPlease input the id of the group: ");
		System.out.println("\f ***ShowGroupMenu ***\n");
		try {
			System.out.println(groupCtr.printGroup(id));
		} catch (RealException e) {
			System.out.println("\f ***ShowGroupMenu ***\n\n" + e.getError());
		}
		inputParser.printReturn();
	}

	private void createGroup() throws RealException {
		int paymentTerm = inputParser
				.getInt("\f ***CreatePersonMenu ***\n\nPlease input the payment term of the new group: ");
		double craftmanDiscount = inputParser
				.getPercent("\f ***CreatePersonMenu ***\n\nPlease input the craftman discount (%) of the new group: ");
		double quantityDiscount = inputParser
				.getPercent("\f ***CreatePersonMenu ***\n\nPlease input the quantity discount (%) of the new group: ");
		double pickUpDiscount = inputParser
				.getPercent("\f ***CreatePersonMenu ***\n\nPlease input the pick up discount (%) of the new group: ");
		try {
			groupCtr.createGroup(paymentTerm, craftmanDiscount,
					quantityDiscount, pickUpDiscount);
		} catch (RealException e) {
			System.out.println("\f ***CreateGroupMenu ***\n\n" + e.getError());
		}
		inputParser.printReturn();
	}

	public void updateGroup() throws RealException {

		int id = inputParser
				.getInt("\f ***UpdateGroupMenu ***\n\nPlease input the id of the group: ");
		groupCtr.getGroup(id);
		boolean found = true;
		try {
			groupCtr.checkGroup(id);
		} catch (RealException e) {
			found = false;
			System.out.println("\f ***UpdateGroupMenu ***\n\n"
					+ e.getError());
			inputParser.printReturn();
		}
		if (found) {
			int choise = 0;
			String value = "";
			boolean exit = false;
			while (!exit) {
				choise = inputParser
						.getInt("\f ***UpdateGroupMenu ***\n\n1. Edit payment term\n2. Edit craftman discount\n3. Edit quantity dicount\n4. Edit pick up discount\n5. Return\n\nMake your choise: ");
				switch (choise) {
				case 1: {
					value = String
							.valueOf(inputParser
									.getInt("\f ***UpdateGroupMenu ***\n\nPlease input the new payment term of the group: "));
					break;
				}
				case 2: {
					value = String
							.valueOf(inputParser
									.getPercent("\f ***UpdateGroupMenu ***\n\nPlease input the new craftman discount (%) of the group: "));
					break;
				}
				case 3: {
					value = String
							.valueOf(inputParser
									.getPercent("\f ***UpdateGroupMenu ***\n\nPlease input the new quantity dicount (%) of the group: "));
					break;
				}
				case 4: {
					value = String
							.valueOf(inputParser
									.getPercent("\f ***UpdateGroupMenu ***\n\nPlease input the new pick up discount (%) of the group: "));
					break;
				}
				case 5: {
					exit = true;
					break;
				}
				}
				if (choise >= 1 && choise <= 4) {
					try {
						groupCtr.updateGroup(id, choise, value);
					} catch (RealException e) {
						System.out.println("\f ***UpdateGroupMenu ***\n\n"
								+ e.getError());
					}
					inputParser.printReturn();
				}
			}
		}
	}

	public void deleteGroup() throws RealException {
		int id = inputParser
				.getInt("\f ***DeleteGroupMenu ***\n\nPlease input the id of the group: ");
		try {
			groupCtr.deleteGroup(id);
		} catch (RealException e) {
			System.out.println("\f ***DeleteGroupMenu ***\n\n" + e.getError());
		}
	}

	public void listAllGroups() {
		ArrayList<String> list = groupCtr.getGroups();
		System.out.println("\f ***ListAllGroupsMenu ***\n");
		for (String line : list) {
			System.out.println(line);
		}
		inputParser.printReturn();
	}
}
